import {
  applyDecorators,
  Body,
  ClassSerializerInterceptor,
  Controller,
  Delete,
  Get,
  HttpException,
  HttpStatus,
  Param,
  ParseIntPipe,
  Post,
  Put,
  UseInterceptors,
  Logger,
  Inject
} from '@nestjs/common';
import {
  ApiOperation,
  ApiParam,
  ApiResponse,
  ApiTags,
  ApiBody,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { CreateUserDto, UpdateUserDto } from 'src/shared/dtos/user.dto';
import { User } from 'src/shared/entities/user.entity';
import { UserService } from 'src/shared/services/user.services';
import { Result } from 'src/shared/vo/result';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';

@Controller('api/users')
@UseInterceptors(ClassSerializerInterceptor)
@ApiTags('api/users')
export class UserController {
  private readonly logger = new Logger(UserController.name);
  // @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly winstonLogger: LoggerService;
  constructor(private readonly userService: UserService) {}
  @Get()
  @ApiFindAll()
  async findALl() {
    this.logger.error('这是Nest内置的日志记录器');
    return this.userService.findAll();
  }
  @Get(':id')
  @ApiFindOne()
  async fundOne(@Param('id', ParseIntPipe) id: number) {
    return this.userService.findOne({ where: { id } });
  }
  @Post()
  @ApiCreate()
  async create(@Body() createUserDto: any) {
    return this.userService.create(createUserDto);
  }
  @Put(':id')
  @ApiUpdate()
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() UpdateUserDto: UpdateUserDto,
  ) {
    const result = await this.userService.update(id, UpdateUserDto);
    if (result.affected) {
      return Result.success('更新成功');
    } else {
      return new HttpException('用户未找到', HttpStatus.NOT_FOUND);
    }
  }
  @Delete(':id')
  @ApiDelete()
  async delete(@Param('id', ParseIntPipe) id: number) {
    const result = await this.userService.delete(id);
    if (result.affected) {
      return Result.success('删除成功');
    } else {
      return new HttpException('用户未找到', HttpStatus.NOT_FOUND);
    }
  }
}

function ApiFindAll() {
  return applyDecorators(
    ApiOperation({ summary: '获取所有的用户列表' }),
    ApiResponse({ status: 200, description: '成功返回用户列表', type: User }),
  );
}

function ApiFindOne() {
  return applyDecorators(
    ApiOperation({ summary: '根据ID获取某个用户信息' }),
    ApiParam({ name: 'id', description: '用户id', type: Number }),
    ApiResponse({ status: 200, description: '成功返回用户信息', type: User }),
    ApiResponse({ status: 404, description: '用户未找到' }),
  );
}

function ApiCreate() {
  return applyDecorators(
    ApiOperation({ summary: '创建新用户' }),
    ApiBearerAuth(),
    ApiBody({ type: CreateUserDto }),
    ApiResponse({ status: 201, description: '用户创建成功', type: User }),
    ApiResponse({ status: 400, description: '请求参数错误' }),
  );
}

function ApiUpdate() {
  return applyDecorators(
    ApiOperation({ summary: '更新用户信息' }),
    ApiBody({ type: CreateUserDto }),
    ApiResponse({
      status: 201,
      description: '用户创建成功',
      type: UpdateUserDto,
    }),
    ApiResponse({ status: 400, description: '请求参数错误' }),
    ApiResponse({ status: 404, description: '用户未找到' }),
  );
}

function ApiDelete() {
  return applyDecorators(
    ApiOperation({ summary: '删除用户' }),
    ApiParam({ name: 'id', description: '用户id', type: Number }),
    ApiResponse({ status: 200, description: '用户删除成功', type: Result }),
    ApiResponse({ status: 400, description: '用户未找到' }),
  );
}
